<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 运能设置类
 * @package  module
 * @subpackage  base/system
 * @author   create by allen.qiang
 * @version 1.0
 */

class sysMax {

    /**
     * 查询配送运能时间
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchDistributionSend( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['uuid']) && is_numeric($arg_array['uuid'])) {
            $sql .= ' AND uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }

        //配送站ID
        if (isset($arg_array['distribution_id']) && !empty($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }

        //配送开始时间
        if (isset($arg_array['send_start_time']) && is_numeric($arg_array['send_start_time'])) {
            $sql .= ' AND send_start_time = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_start_time'];
        }

        //配送结束时间
        if (isset($arg_array['send_end_time']) && is_numeric($arg_array['send_end_time'])) {
            $sql .= ' AND send_end_time = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_end_time'];
        }

        //运单上限
        if (isset($arg_array['max_bill']) && is_numeric($arg_array['max_bill'])) {
            $sql .= ' AND max_bill = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['max_bill'];
        }

        //状态
        if (isset($arg_array['send_status']) && in_array($arg_array['send_status'], array(0, 1))) {
            $sql .= ' AND send_status = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['send_status'];
        }



        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_distribution_send ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_distribution_send ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'creator' => $item->creator,
                    'distribution_id' => $item->distribution_id,
                    'send_start_time' => $item->send_start_time,
                    'send_end_time' => $item->send_end_time,
                    'max_bill' => $item->max_bill,
                    'send_status' => $item->send_status
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询配送运能
     *
     * @param unknown_type $arg_array
     */
    public function searchSaleShow( $arg_array ){

        global $G_SHOP;

        //检验参数
        if (!isset($arg_array['distribution_id']) || !is_numeric($arg_array['distribution_id'])) {
            return array('code' => 400, 'message' => '参数错误');
        }

        //数据库查询
        quanli_include('module/base/system/sysDistribution.class.php');
        $sysDistributionr = new sysDistribution( $G_SHOP->DBCA );
        $sale_array = array(
            'distribution_id' => $arg_array['distribution_id']
        );
        $sale_result = $sysDistributionr->search( $sale_array );
        if (!$sale_result) {
            return array('code' => 400, 'message' => '配送站信息未找到');
        }
        $sale_result = $sale_result[0];

        $sale_array['sortname'] = 'send_start_time';
        $sale_array['sortorder'] = 'ASC';
        $space_result = $this->searchDistributionSend( $sale_array );
        if (!$space_result) {
            $sale_result['space'] = '';
        } else {
            $sale_result['space'] = $space_result;
        }

        return array('code' => 200, 'message' => '查询成功', 'data' => $sale_result);
    }



    /**
     * 添加运能设置
     * @param $arg_array 参数数组
     * @return array|null
     */

    public function editDistributionSend ( $arg_array ) {


        global $G_SHOP;

        //检查参数

        if (!isset( $arg_array['params'])){
            return array('message'=>'参数错误','code'=> 400);
        }

        //传递的参数
        $params = $arg_array['params'];

        //配送站ID
        $distribution_id = $params['distribution_id'];

        //配送时间控制
        if (isset($params) && !empty($params)) {

            for ($i=1; $i <= $params['sameCount']; $i++) {
                $send_start_time = isset($params['send_start_time_' . $i]) ? $params['send_start_time_' . $i] : '';
                $send_end_time = isset($params['send_end_time_' . $i]) ? $params['send_end_time_' . $i] : '';
                $max_bill = isset($params['max_bill_' . $i]) ? $params['max_bill_' . $i] : '';
                $send_status = isset($params['send_status_' . $i]) ? $params['send_status_' . $i] : '';

                if (empty($send_start_time) && empty($send_end_time) && empty($max_bill)) {
                    continue;
                }

//                if(!empty($send_status)){
//                    return array('code' => 404, 'message' => '请选择状态');
//                }

                if (empty($send_start_time) || empty($send_end_time) || empty($max_bill)) {
                    return array('code' => 404, 'message' => '请填写完整的配送时间控制数据');
                }

                $dTime_array[] = array($send_start_time, $send_end_time, $max_bill, $send_status);
            }
            if (empty($dTime_array)) {
                return array('code' => 404, 'message' => '请填写完整的配送时间控制数据');
            }
        }




        $creator = $arg_array['creator'];

        try{

            $G_SHOP->DBCA->beginTransaction();

            // 查询配送站信息
            $distribution_obj = new entity('sys_distribution');
            $distribution_obj->distribution_id = $distribution_id;
            if(!$G_SHOP->DBCA->fetch($distribution_obj)){
                returnJsonData("配送站不存在，请刷新页面重试", 404);
            }

            //保存前清除旧数据
            $G_SHOP->DBCA->stmt_query('DELETE FROM sys_distribution_send WHERE distribution_id = ?', 'i', $distribution_id);
            //保存配送运能信息
            if (isset($params) && !empty($params)) {
                if (!empty($dTime_array)) {
                    foreach ($dTime_array as $dT) {
                        $distribution_send = new entity('sys_distribution_send');
                        $distribution_send->creator = $creator;
                        $distribution_send->distribution_id = $distribution_id;
                        $distribution_send->send_start_time = $dT[0];
                        $distribution_send->send_end_time = $dT[1];
                        $distribution_send->max_bill = $dT[2];
                        $distribution_send->send_status = $dT[3];
                        $G_SHOP->DBCA->save($distribution_send);
                    }
                }
            }



            $G_SHOP->DBCA->commit();

            returnJsonData('保存成功', 200);

        }catch (Exception $e) {
            if ($e->getCode() > 0){

                $G_SHOP->DBCA->rollBack();

                returnJsonData( $e->getCode(), $e->getMessage() );
            }else {
                error_log($e->getMessage());
                returnJsonData( 500, '系统错误' );

            }
        }
    }


}